package com.example.woke1;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.PswchangeActivity;
import com.zhongcai.online.R;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_zhcailidingqi;
import com.woke.diyview.RoundProgressBar1;
import com.woke.method.MyApp;
import com.wokeMy.view.turui.MyRealNameActivity;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ZhcailicaiActivity extends Activity implements OnClickListener, OnItemClickListener {

	private ListView mListView;
	private Intent intent;
	private MyApp application;
	private AlertDialog alertDialog;
	private TextView mTlili;
	private View mProgress;
	private ArrayList<Data_zhcailidingqi> datalist = new ArrayList<Data_zhcailidingqi>();
	private Myadapter adapter;
	private View headview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhcailicai);
		
		application = (MyApp) getApplication();
		intent = new Intent();
		intview();
	}

	private void intview() {
		headview = getLayoutInflater().inflate(R.layout.headview_zhcailist, null);
		findViewById(R.id.avzhcailicai_image_back).setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.avzhcailicai_listview);
		headview.findViewById(R.id.avzhcailicai_text_huoqi).setOnClickListener(this);
		mTlili = (TextView) headview.findViewById(R.id.avzhcailicai_text_lili);//�в�����
		mProgress = findViewById(R.id.avzhcailicai_progress);
		mListView.addHeaderView(headview);
		mListView.setOnItemClickListener(this);
		getlilvdata();
		getdingqidatalist();
	}
	
	private void getdingqidatalist() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcailicaiActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "financial");
		params.put("a", "dingqiList");
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						JSONArray jsonArray = object.getJSONArray("data");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject jsonObject = jsonArray.getJSONObject(i);
							String money = jsonObject.getString("money");
							String id = jsonObject.getString("id");
							String date = jsonObject.getString("date");
							String exp_money = jsonObject.getString("exp_money");
							String status = jsonObject.getString("status");
							String interest = jsonObject.getString("interest");
							String product_name = jsonObject.getString("product_name");
							Data_zhcailidingqi list = new Data_zhcailidingqi(money,id, date,
									exp_money,status,interest,product_name);
							datalist.add(list);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if(datalist.size() > 0){
					adapter = new Myadapter();
					mListView.setAdapter(adapter);
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mProgress.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mProgress.setVisibility(View.GONE);
			}
		});
	}

	private void getlilvdata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(ZhcailicaiActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "financial");
		params.put("a", "huoqi");
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						String zhcaililv = object.getString("data");
						mTlili.setText(zhcaililv+"%");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mProgress.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mProgress.setVisibility(View.GONE);
			}
		});
	}

	class Myadapter extends  BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datalist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		class Holder {
//			ImageView mIicon;
			RoundProgressBar1 mBraepr;
			TextView mTdate;
			TextView mTlilv;
			TextView mTmoney;
			TextView mTtitle;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = getLayoutInflater().inflate(R.layout.item_zhcailicai_adater, null);
//				holder.mIicon = (ImageView) convertView.findViewById(R.id.item_myblind_bankicon);
				holder.mBraepr = (RoundProgressBar1) convertView.findViewById(R.id.item_zhcailicai_progerss);
				holder.mTdate = (TextView) convertView.findViewById(R.id.item_zhcailicai_textdate);
				holder.mTlilv = (TextView) convertView.findViewById(R.id.item_zhcailicai_textlilv);
				holder.mTmoney = (TextView) convertView.findViewById(R.id.item_zhcailicai_textmoney);
				holder.mTtitle= (TextView) convertView.findViewById(R.id.item_zhcailicai_texttitle);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			
			Data_zhcailidingqi data = datalist.get(position);
			holder.mTdate.setText(data.date+"天");
			holder.mTlilv.setText(data.interest+"%");
			holder.mTmoney.setText(data.money+"元");
			holder.mTtitle.setText(data.product_name);
			
			float parseFloat = Float.parseFloat(data.exp_money);
			float money = Float.parseFloat(data.money);
			Log.e("parseFloat", ""+parseFloat);
			if(parseFloat == 0f){
				holder.mBraepr.setProgress(100);
			}else if(parseFloat > 0){
				int progress = (int) (((money - parseFloat) / money)*100);
				if(progress == 0){
					if(parseFloat == money){
						holder.mBraepr.setProgress(0);
					}else{
						holder.mBraepr.setProgress(1);
					}
				}else{
					holder.mBraepr.setProgress(progress);
				}
			}
			
			return convertView;
		}
	}
	
	private void aralogid2(String info,String kll,final int ppp){
		   alertDialog = new AlertDialog.Builder(ZhcailicaiActivity.this). 
	                setTitle(info). 
	                setMessage(kll). 
//	                setIcon(R.drawable.ic_launcher). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	if(ppp == 3){
	                    		intent.setClass(ZhcailicaiActivity.this,MyRealNameActivity.class);
	                    		startActivity(intent);
	                    	}else if(ppp == 1){
	                    		intent.setClass(ZhcailicaiActivity.this, PswchangeActivity.class);
	                    		intent.putExtra("name", 0);
	                    		startActivity(intent);
	                    	}
	                    		alertDialog.dismiss();
	                    } 
	                }). 
	                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	alertDialog.dismiss();
	                        // TODO Auto-generated method stub  
	                    } 
	                }).create(); 
	        alertDialog.show(); 
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.avzhcailicai_image_back:
			finish();
			break;
			
		case R.id.avzhcailicai_text_huoqi:
			if(application.getDatas_load ()==null)return;
			if(application.getDatas_load().getUser_no_status().equals("1")){
				aralogid2("还未实名认证","前往认证？",3);
//				Toast.makeText(BorrowinfoActivity.this, "��δʵ����֤", Toast.LENGTH_SHORT).show();
			}else if(application.getDatas_load().getUser_no_status().equals("2")){
//				aralogid2("��δʵ����֤","ǰ����֤��",3);
				Toast.makeText(ZhcailicaiActivity.this, "实名认证审核中，无操作权限", Toast.LENGTH_SHORT).show();
			}else if(application.getDatas_load().getPay_pwd().equals("")){
				Toast.makeText(ZhcailicaiActivity.this, "您还未设置支付密码！", Toast.LENGTH_SHORT).show();
				aralogid2("还未设置支付密码","前往设置？",1);
			}else{
				intent.setClass(ZhcailicaiActivity.this, ZhcaihuoqiActivity.class);
				startActivity(intent);
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if(application.getDatas_load ()==null)return;
		if(application.getDatas_load().getUser_no_status().equals("1")){
			aralogid2("还未实名认证","前往认证？",3);
//			Toast.makeText(BorrowinfoActivity.this, "��δʵ����֤", Toast.LENGTH_SHORT).show();
		}else if(application.getDatas_load().getUser_no_status().equals("2")){
//			aralogid2("��δʵ����֤","ǰ����֤��",3);
			Toast.makeText(ZhcailicaiActivity.this, "实名认证审核中，无操作权限", Toast.LENGTH_SHORT).show();
		}else if(application.getDatas_load().getPay_pwd().equals("")){
			Toast.makeText(ZhcailicaiActivity.this, "您还未设置支付密码！", Toast.LENGTH_SHORT).show();
			aralogid2("还未设置支付密码","前往设置？",1);
		}else{
			if(arg2 > 0){
				intent.setClass(ZhcailicaiActivity.this, ZhcaidingqiActivity.class);
				intent.putExtra("id", datalist.get(arg2-1).id);//����id
				intent.putExtra("fromwhere", false);
				startActivity(intent);
			}
		}
	}
}
